package com.beiye.drivertransport.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;

/**
 * Created by ky on 2018/1/15.
 */

public class TimeUtils {
    private StringBuilder mFormatBuilder;
    private Formatter mFormatter;


    public TimeUtils() {
        // 转换成字符串的时间
        mFormatBuilder = new StringBuilder();
        mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());


    }


    /**
     * 把毫秒转换成：1:20:30这里形式
     * @param timeMs
     * @return
     */
    public String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;
        int seconds = totalSeconds % 60;


        int minutes = (totalSeconds / 60) % 60;


        int hours = totalSeconds / 3600;


        mFormatBuilder.setLength(0);

        return mFormatter.format("%02d:%02d:%02d", hours, minutes, seconds).toString();
    }

    public static String monthFirstDay(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        String dateStr=format.format(cale.getTime())+" 00:00:00";
        return dateStr;
    }

    public static String monthEndDay(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        String dateStr=format.format(cale.getTime())+" 23:59:59";
        return dateStr;
    }
}
